/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmNewRow.java
 *
 * Created on 1 Οκτ 2011, 9:12:55 μμ
 */
package routing;

import java.awt.Component;
import routing.Enumerators.TableNames;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;

/**
 *
 * @author elias
 */
public class frmNewRow extends javax.swing.JFrame {

    
    List<Integer> NodesID=new ArrayList<Integer>();
   
    public frmNewRow(String Tab) {
        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        if (Tab.equals("Areas")){
            SelectionTabPanel.setSelectedIndex(SelectionTabPanel.indexOfTab("Areas"));
            SelectionTabPanel.setEnabledAt(SelectionTabPanel.indexOfTab("Nodes"), false);
        }else if(Tab.equals("Nodes")){
            SelectionTabPanel.setSelectedIndex(SelectionTabPanel.indexOfTab("Nodes"));
            SelectionTabPanel.setEnabledAt(SelectionTabPanel.indexOfTab("Areas"), false);
            FillFrmCmbs();
         //   bxArea.doClick();
        }
    }
    
   public void SetTxt_Freq(String Freq){
       Txt_Freq.setText(Freq);
   } 
    public final void FillFrmCmbs(){
         DbConnection db=new DbConnection();
            Connection conn=db.Connect();
   try{
         ResultSet rs=db.SelectFromDb(TableNames.Areas,"", conn );
   if (rs!=null){
      while(rs.next()){
         SlArea.addItem(rs.getString("ID"));
     }
      conn.close();
      rs.close();
   }
    }catch(SQLException e){
    e.printStackTrace();
}
   FillNeighComboBoxes();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SelectionTabPanel = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        bxArea = new javax.swing.JCheckBox();
        SlArea = new javax.swing.JComboBox();
        NeighbourPanel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        ComboPlusNodes = new javax.swing.JComboBox();
        btnPlusNodes = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        AddedComboNeigh = new javax.swing.JComboBox();
        Txt_Freq = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        TogglePanel = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jToggleButton4 = new javax.swing.JToggleButton();
        jToggleButton5 = new javax.swing.JToggleButton();
        jToggleButton6 = new javax.swing.JToggleButton();
        jToggleButton7 = new javax.swing.JToggleButton();
        jToggleButton8 = new javax.swing.JToggleButton();
        jToggleButton9 = new javax.swing.JToggleButton();
        jToggleButton10 = new javax.swing.JToggleButton();
        jToggleButton11 = new javax.swing.JToggleButton();
        jToggleButton12 = new javax.swing.JToggleButton();
        jToggleButton13 = new javax.swing.JToggleButton();
        jToggleButton14 = new javax.swing.JToggleButton();
        jToggleButton15 = new javax.swing.JToggleButton();
        jToggleButton16 = new javax.swing.JToggleButton();
        jToggleButton17 = new javax.swing.JToggleButton();
        jToggleButton18 = new javax.swing.JToggleButton();
        jToggleButton19 = new javax.swing.JToggleButton();
        jToggleButton20 = new javax.swing.JToggleButton();
        jToggleButton21 = new javax.swing.JToggleButton();
        btnAddNode = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        bxArea.setText("Area Flag");
        jPanel1.add(bxArea, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 120, 97, -1));
        jPanel1.add(SlArea, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 150, 70, -1));

        NeighbourPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setForeground(new java.awt.Color(255, 0, 51));
        jLabel3.setText("Pick Neighbours");
        NeighbourPanel.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 145, -1));

        jLabel4.setText("Available Neighbours");
        NeighbourPanel.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 33, -1, -1));

        jLabel5.setText("Added Neighbours");
        NeighbourPanel.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 108, -1, -1));
        NeighbourPanel.add(ComboPlusNodes, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 50, -1));

        btnPlusNodes.setText("+");
        btnPlusNodes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlusNodesActionPerformed(evt);
            }
        });
        NeighbourPanel.add(btnPlusNodes, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 60, -1, 20));

        jLabel7.setText("Nodes");
        NeighbourPanel.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 60, -1, -1));
        NeighbourPanel.add(AddedComboNeigh, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, 140, -1));

        jPanel1.add(NeighbourPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 110, 157, 220));

        Txt_Freq.setEditable(false);
        Txt_Freq.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                Txt_FreqKeyTyped(evt);
            }
        });
        jPanel1.add(Txt_Freq, new org.netbeans.lib.awtextra.AbsoluteConstraints(141, 72, 100, -1));

        jLabel8.setText("Frequency");
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 75, -1, -1));

        jButton1.setText("...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 70, 30, -1));

        SelectionTabPanel.addTab("Nodes", jPanel1);

        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("                            Pick Frequencies");
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(39, 108, 246, 19));

        TogglePanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jToggleButton1.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton1.setText("40");
        TogglePanel.add(jToggleButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 29));

        jToggleButton2.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton2.setText("42");
        TogglePanel.add(jToggleButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(94, 0, -1, 29));

        jToggleButton3.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton3.setText("43");
        TogglePanel.add(jToggleButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(141, 0, -1, 29));

        jToggleButton4.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton4.setText("41");
        TogglePanel.add(jToggleButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(47, 0, -1, 29));

        jToggleButton5.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton5.setText("46");
        TogglePanel.add(jToggleButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(282, 0, -1, 29));

        jToggleButton6.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton6.setText("45");
        TogglePanel.add(jToggleButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(235, 0, -1, 29));

        jToggleButton7.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton7.setText("44");
        TogglePanel.add(jToggleButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(188, 0, -1, 29));

        jToggleButton8.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton8.setText("53");
        TogglePanel.add(jToggleButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(282, 35, -1, 29));

        jToggleButton9.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton9.setText("52");
        TogglePanel.add(jToggleButton9, new org.netbeans.lib.awtextra.AbsoluteConstraints(235, 35, -1, 29));

        jToggleButton10.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton10.setText("51");
        TogglePanel.add(jToggleButton10, new org.netbeans.lib.awtextra.AbsoluteConstraints(188, 35, -1, 29));

        jToggleButton11.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton11.setText("50");
        TogglePanel.add(jToggleButton11, new org.netbeans.lib.awtextra.AbsoluteConstraints(141, 35, -1, 29));

        jToggleButton12.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton12.setText("49");
        TogglePanel.add(jToggleButton12, new org.netbeans.lib.awtextra.AbsoluteConstraints(94, 35, -1, 29));

        jToggleButton13.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton13.setText("48");
        TogglePanel.add(jToggleButton13, new org.netbeans.lib.awtextra.AbsoluteConstraints(47, 35, -1, 29));

        jToggleButton14.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton14.setText("47");
        TogglePanel.add(jToggleButton14, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 35, -1, 29));

        jToggleButton15.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton15.setText("60");
        TogglePanel.add(jToggleButton15, new org.netbeans.lib.awtextra.AbsoluteConstraints(282, 70, -1, 29));

        jToggleButton16.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton16.setText("59");
        TogglePanel.add(jToggleButton16, new org.netbeans.lib.awtextra.AbsoluteConstraints(235, 70, -1, 29));

        jToggleButton17.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton17.setText("58");
        TogglePanel.add(jToggleButton17, new org.netbeans.lib.awtextra.AbsoluteConstraints(188, 70, -1, 29));

        jToggleButton18.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton18.setText("57");
        TogglePanel.add(jToggleButton18, new org.netbeans.lib.awtextra.AbsoluteConstraints(141, 70, -1, 29));

        jToggleButton19.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton19.setText("56");
        TogglePanel.add(jToggleButton19, new org.netbeans.lib.awtextra.AbsoluteConstraints(94, 70, -1, 29));

        jToggleButton20.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton20.setText("55");
        TogglePanel.add(jToggleButton20, new org.netbeans.lib.awtextra.AbsoluteConstraints(47, 70, -1, 29));

        jToggleButton21.setFont(new java.awt.Font("Tahoma", 0, 8));
        jToggleButton21.setText("54");
        TogglePanel.add(jToggleButton21, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 70, -1, 29));

        jPanel2.add(TogglePanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(7, 133, 322, 100));

        SelectionTabPanel.addTab("Areas", jPanel2);

        btnAddNode.setText("Add");
        btnAddNode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddNodeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(SelectionTabPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(153, 153, 153)
                        .addComponent(btnAddNode)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SelectionTabPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 403, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAddNode)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //Set Plus Combo Boxes
    public void FillNeighComboBoxes(){
        DbConnection db=new DbConnection();
        Connection conn=db.Connect();
        try{
        
        ResultSet  rs=db.SelectFromDb(TableNames.Nodes,"", conn);
                while(rs.next()){
                        ComboPlusNodes.addItem(rs.getString("ID"));                  
                }
            rs.close();
            conn.close();
        }catch(SQLException e){
        e.printStackTrace();
        }
    }
    
private void btnAddNodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddNodeActionPerformed

if(SelectionTabPanel.getSelectedIndex()==SelectionTabPanel.indexOfTab("Nodes")){
   DbConnection db=new DbConnection();
   Connection conn=db.Connect(); 
   try{
   int KeyNodesCount=db.ReturnUniqueKey(TableNames.Nodes, conn);
   //NODES TAB
    if (bxArea.isSelected()){
       if(SlArea.getItemCount()>0){
       db.AddToDb( "INSERT INTO Nodes (ID,Area_flag,Area_ID,Frequency) VALUES (" + (KeyNodesCount+1)+"," + "1," + SlArea.getSelectedItem()+ "," + Integer.parseInt(Txt_Freq.getText()) +")", conn);
       FrmNodes.NodesTm.fireTableDataChanged();
       }else{
           JOptionPane.showMessageDialog(this, "You should pick an area to include the node", "Warning", JOptionPane.ERROR_MESSAGE);
           conn.close();
       }
   }
    else{
       db.AddToDb( "INSERT INTO Nodes (ID,Area_flag,Area_ID,Frequency) VALUES (" + (KeyNodesCount+1)+"," + "0,-1," + Integer.parseInt(Txt_Freq.getText()) +")", conn);
       FrmNodes.NodesTm.fireTableDataChanged();
    }
    //CreaTingGeolocationDB
    for (int i=0;i<NodesID.size();i++){
        int GeoDBCount=db.ReturnUniqueKey(TableNames.GeolocationDb, conn);
        db.AddToDb( "INSERT INTO GeolocationDB (ID,NodeID,NeighbourID) VALUES ("+(GeoDBCount+1)+","+ (KeyNodesCount+1)+","+NodesID.get(i)+")", conn);
        //REVERT
        db.AddToDb( "INSERT INTO GeolocationDB (ID,NodeID,NeighbourID) VALUES ("+(GeoDBCount+2)+","+ NodesID.get(i)+","+(KeyNodesCount+1)+")", conn);
    }
    NodesID.clear();
   if(!conn.isClosed()){
       conn.close();
   }
   }catch(SQLException e){
       e.printStackTrace();
   }
   this.dispose();
   //AREAS TAB 
}else if(SelectionTabPanel.getSelectedIndex()==SelectionTabPanel.indexOfTab("Areas")){
    List<Integer> Frequencies=new ArrayList<Integer>(); 
    for(Component comp:TogglePanel.getComponents()){
        JToggleButton togg=(JToggleButton)comp;
        if(togg.isSelected()){
            Frequencies.add(Integer.parseInt(togg.getText()));
        }
    }
    DbConnection db=new DbConnection();
    Connection conn=db.Connect();
    int AreasCount=db.ReturnUniqueKey(TableNames.Areas, conn);
    db.AddToDb("INSERT INTO Areas(ID) VALUES("+(AreasCount+1)+")", conn);
    for(Integer i:Frequencies){
            db.AddToDb("INSERT INTO AreaFrequencies(ID,Frequency) VALUES("+(AreasCount+1)+","+i+")", conn);
    }
    frmAreas.AreasTm.fireTableDataChanged();
    this.dispose();

}


}//GEN-LAST:event_btnAddNodeActionPerformed

private void btnPlusNodesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlusNodesActionPerformed
 if(ComboPlusNodes.getSelectedItem()!=null){
     AddedComboNeigh.addItem(" Node with ID: "+ComboPlusNodes.getSelectedItem());
     NodesID.add(Integer.parseInt(ComboPlusNodes.getSelectedItem().toString()));
    }
}//GEN-LAST:event_btnPlusNodesActionPerformed

private void Txt_FreqKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Txt_FreqKeyTyped
char c=evt.getKeyChar();
if ((Character.isDigit(c)) || (c==KeyEvent.VK_BACK_SPACE) || (c==KeyEvent.VK_DELETE)){
    
}else{
    evt.consume();
}
}//GEN-LAST:event_Txt_FreqKeyTyped

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
if (bxArea.isSelected()){
frmFreq frq=new frmFreq(Integer.parseInt((String)SlArea.getSelectedItem()),this);
frq.setLocationRelativeTo(jButton1);
frq.setTitle("Pick Frequency");
frq.setVisible(true);    
}else{
frmFreq frq=new frmFreq(0,this);
frq.setLocationRelativeTo(jButton1);
frq.setTitle("Pick Frequency");
frq.setVisible(true);
}
    
}//GEN-LAST:event_jButton1ActionPerformed



private boolean ValidateInput(String Tab){
    
    return true;
}





    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AddedComboNeigh;
    private javax.swing.JComboBox ComboPlusNodes;
    private javax.swing.JPanel NeighbourPanel;
    private javax.swing.JTabbedPane SelectionTabPanel;
    private javax.swing.JComboBox SlArea;
    private javax.swing.JPanel TogglePanel;
    public javax.swing.JTextField Txt_Freq;
    private javax.swing.JButton btnAddNode;
    private javax.swing.JButton btnPlusNodes;
    private javax.swing.JCheckBox bxArea;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton10;
    private javax.swing.JToggleButton jToggleButton11;
    private javax.swing.JToggleButton jToggleButton12;
    private javax.swing.JToggleButton jToggleButton13;
    private javax.swing.JToggleButton jToggleButton14;
    private javax.swing.JToggleButton jToggleButton15;
    private javax.swing.JToggleButton jToggleButton16;
    private javax.swing.JToggleButton jToggleButton17;
    private javax.swing.JToggleButton jToggleButton18;
    private javax.swing.JToggleButton jToggleButton19;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton20;
    private javax.swing.JToggleButton jToggleButton21;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToggleButton jToggleButton4;
    private javax.swing.JToggleButton jToggleButton5;
    private javax.swing.JToggleButton jToggleButton6;
    private javax.swing.JToggleButton jToggleButton7;
    private javax.swing.JToggleButton jToggleButton8;
    private javax.swing.JToggleButton jToggleButton9;
    // End of variables declaration//GEN-END:variables
}
